#!/bin/sh
# install.sh
# Prentice Hall CyberClassroom Solaris installation script.
# Deitel C++ How To Program 2e

SCRIPT_NAME="$0"

# FUNCTION:     cleanup()
# DESCRIPTION:  To uninstall any aborted attempted install, cleanup the "footprints" of any thing the
#               install did to the user's system, and return with the specified error code.
# ARGUMENTS:    $1 - The error code to exit this script with.
# GLOBALS:      Assumes $UNINSTALL_LINE1 $UNINSTALL_LINE2 $UNINSTALL_LINE3 $UNINSTALL_LINE4 are set if need be.
# RETURN VALUE: NONE
cleanup()
{
  echo
  echo "$SCRIPT_NAME: Installation Aborted, cleaning up..."
  echo
  $UNINSTALL_LINE1
  $UNINSTALL_LINE2
  $UNINSTALL_LINE3
  $UNINSTALL_LINE4
  exit $1
}


# FUNCTION:     two_value_question()
# DESCRIPTION   As a question of the user for input that has 2 allowable answers.
#               The first argument is the first allowable answer the user can type in and
#               also the default answer if the user just hits the enter key. The second argument
#               is the other allowable answer.  The comparison is case INDEPENDENT, in other
#               words the answer "YeS" is the same as "yes".  Also the first letter of the answer
#               is all that is required, however if the user chooses to type the full allowable answer
#               then ONLY the first character is checked, in other words "Yasdfa" will be interpreted
#               as a valid "YES".
#               
# ARGUMENTS:    $1 - first allowable value and also the prompt default
#               $2 - Second allowable value
# GLOBALS:      Assumes $L1 $L2 $L3 $L4 $L5 $L6 $L7 are set if need be.
# RETURN VALUE: $PROMPT_ANSWER set a the first character of the selected prompt.
two_value_question()
{
  FIRST_CHAR_OF_ARG1=`echo $1 | tr "[:lower:]" "[:upper:]" | awk '{print substr($1,1,1)}'` 
  FIRST_CHAR_OF_ARG2=`echo $2 | tr "[:lower:]" "[:upper:]" | awk '{print substr($1,1,1)}'` 
  QUIT_WHILE=0
  while [ $QUIT_WHILE -eq 0 ]
  do
    echo
    if [ "$L1" != "" ]; then
      echo $L1
    fi
    if [ "$L2" != "" ]; then
      echo $L2
    fi
    if [ "$L3" != "" ]; then
      echo $L3
    fi
    if [ "$L4" != "" ]; then
      echo $L4
    fi
    if [ "$L5" != "" ]; then
      echo $L5
    fi
    if [ "$L6" != "" ]; then
      echo $L6
    fi
    if [ "$L7" != "" ]; then
      echo $L7
    fi
    read A_OR_B
    if [ "$A_OR_B" = "" ]; then
      A_OR_B=$1
    fi
    PROMPT_ANSWER=`echo $A_OR_B | tr "[:lower:]" "[:upper:]" | awk '{print substr($1,1,1)}'`
    if [ "$PROMPT_ANSWER" != "$FIRST_CHAR_OF_ARG1" -a "$PROMPT_ANSWER" != "$FIRST_CHAR_OF_ARG2" ]; then
      echo
      echo "Please answer with \"$1\"" or \"$2\"".  Try again."
    else
      QUIT_WHILE=1
    fi
  done
  return 0
}


trap 'echo;echo "Ctrl-C detected";echo;cleanup 1' 2

CYBERCLASSROOM_JRE_VERSION="1.1.5"
CYBERCLASSROOM_JRE_DIRECTORY="jre1.1.5"
CYBERCLASSROOM_VIEWER_VERSION="1.2.0"
#SOLARIS_JRE_RUNTIME=jre115-solaris2-sparc.exe
SOLARIS_JRE_RUNTIME=jre115s.exe
CDROM_DIR=`/usr/bin/pwd`
CYBERCLASSROOM_BOOK_DIR=cpphtp2
CYBERCLASSROOM_BOOK_FILE=cpphtp2.jar
CYBERCLASSROOM_INITIALIZATION_FILE=webdb.ini
CYBERCLASSROOM_HTML_FILE=cpphtp2.htm
CYBERCLASSROOM_VIEWER_DIR=viewer
CYBERCLASSROOM_BOOK_SCRIPT=cpphtp2.sh
CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT=uninstall.sh
# This book requires at least 350MB of disk space for Full Installation.
CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_MB_FOR_FULL=350
CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_MB_FOR_COMPACT=35
CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K_FOR_FULL=358400
CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K_FOR_COMPACT=35840

UNINSTALL_LINE1=""
UNINSTALL_LINE2=""
UNINSTALL_LINE3=""
UNINSTALL_LINE4=""

echo
echo "********************************************************************"
echo "** Welcome to the Prentice Hall CyberClassroom Installation Script *"
echo "**                                                                 *"
echo "** Please follow the prompts where requested.  Options for each    *"
echo "** prompt are enclosed inside circular parenthesis () characters.  *"
echo "** Only the first character for each prompt is necessary, such as  *"
echo "** \"y\" for \"yes\" and \"n\" for \"no\". Default values for each         *"
echo "** prompt are enclosed inside [] characters.                       *"
echo "********************************************************************"

# No arguments required for this script
if [ $# -ne 0 ]; then
  echo
  echo "Note: no arguments are required for this installation script."
  echo "The arguments will be ignored."
  echo
fi

# Warning message if the current user is not "root"
if [ "$USER" != "root" ]; then
  L5=""; L6=""; L7=""
  L1="WARNING: This installation script creates files and directories "
  L2="on local mount points.  Unless you have proper file permissions "
  L3="the installation may not proceed properly."
  L4="Continue installation as user \"$USER\" ? (yes|no) [no]: \c"
  two_value_question no yes 
  if [ "$PROMPT_ANSWER" != "Y" ]; then
    cleanup 5
  fi
  echo
fi


INSTALL_JRE=1
JRE_LOCATION=/opt


L2=""; L3=""; L4=""; L5=""; L6=""; L7=""
L1="Do you have the SunSoft JRE installed ? (yes|no) [no]: \c"
two_value_question no yes
if [ "$PROMPT_ANSWER" = "Y" ]; then
  while [ 1 ]
  do
    JRE_LOCATION=/opt
    INSTALL_JRE=0

    # Ask for the "jre" in the current path
    echo
    echo "Where is the SunSoft JRE installed ? (ex: \"/opt/apps/jre${CYBERCLASSROOM_JRE_VERSION}\"): \c"
    read RESPONSE
    if [ "$RESPONSE" = "" ]; then
      echo
      echo "You must type a location, such as \"/opt/apps/jre${CYBERCLASSROOM_JRE_VERSION}\".  Try again."
      echo
      continue
    fi

    JRE_LOCATION=$RESPONSE

    # Check to see if the user is right
    if [ ! -d $JRE_LOCATION ]; then
        echo
        echo "The directory \"$JRE_LOCATION\" does not exist on your system."
        echo "Please try another path."
        echo
        continue
    fi

    # OK Now we have the location
    JRE_EXECUTABLE=$JRE_LOCATION/bin/jre

    # Check for the JRE and run it, make sure its the version
    # CyberClassroom needs or a version
    # that is later.
    if [ ! -f $JRE_EXECUTABLE ]; then
      TMP_JRE_LOCATION=$JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION}
      TMP_JRE_EXECUTABLE=$TMP_JRE_LOCATION/bin/jre
      if [ ! -f $TMP_JRE_EXECUTABLE ]; then
        L5=""; L6=""; L7=""
        L1="The executable \"$TMP_JRE_EXECUTABLE\" does not exist "
        L2="Maybe there is a damaged installation of a previous JRE."
        L3="Do you wish to reinstall the JRE in the directory"
        L4="\"$JRE_LOCATION\" ? (yes/no) [yes]: \c"
        two_value_question yes no
        if [ "$PROMPT_ANSWER" = "Y" ]; then
          INSTALL_JRE=1
          break
        else
          L2=""; L3=""; L4=""; L5=""; L6=""; L7=""
          L1="Choose another location? (yes/no) [yes]: \c"
          two_value_question yes no
          if [ "$PROMPT_ANSWER" = "Y" ]; then
            continue
          else
            echo
            echo "$0: ERROR: A possibly damaged installation of the JRE was detected"
            echo "in the directory \"$JRE_LOCATION\"."
            echo "Please correct this problem and run this installation again."
            cleanup 15
          fi
        fi
      else
        JRE_LOCATION=$TMP_JRE_LOCATION
        JRE_EXECUTABLE=$TMP_JRE_EXECUTABLE
      fi
    fi

    CURRENT_JRE=`$JRE_EXECUTABLE -help 2>&1 | grep -i Version | awk '{print $NF}'`
    if [ `echo $CURRENT_JRE | awk -F. '{print $1$2$3$4}'` -ne `echo $CYBERCLASSROOM_JRE_VERSION | awk -F. '{print $1$2$3$4}'` ]; then
      L7=""
      L1="The JRE executable [$CURRENT_JRE] on your system"
      L2="is not the version used by the CyberClassroom JRE "
      L3="[jre version \"$CYBERCLASSROOM_JRE_VERSION\"]."
      L4="If you are using a version older than this,"
      L5="it is recommended you install the CyberClassroom JRE version as well."
      L6="Continue and use existing JRE  ? (yes/no) [no]: \c"
      two_value_question no yes
      if [ "$PROMPT_ANSWER" = "Y" ]; then
        INSTALL_JRE=0
      else
        INSTALL_JRE=1
      fi
      break
    else
      break
    fi

  done

fi

# If the JRE needs installing do it now

while [ 1 ]
do
if [ $INSTALL_JRE -eq 1 ]; then
  echo
  echo "You are about to install the SunSoft JRE version $CYBERCLASSROOM_JRE_VERSION."
  echo "Please conform to all licensing agreements."
  echo "NOTE: The installer will create a directory named (jre${CYBERCLASSROOM_JRE_VERSION})"
  echo "      inside the directory you specify.  For example, if you specify \"/opt\" in"
  echo "      response to the prompt below, the JRE will be installed in directory "
  echo "      \"/opt/jre${CYBERCLASSROOM_JRE_VERSION}\""
  echo "Where do we install the SunSoft JRE (full directory path) ? [$JRE_LOCATION]: \c"
  read RESPONSE
  if [ "$RESPONSE" != "" ]; then
    JRE_LOCATION=$RESPONSE
  fi
  L2=""; L3=""; L4=""; L5=""; L6=""; L7=""
  L1="Install SunSoft JRE in directory ($JRE_LOCATION) creating if necessary ? (yes/no) [yes]: \c  "
  two_value_question yes no
  if [ "$PROMPT_ANSWER" = "N" ]; then
    L2=""; L3=""; L4=""; L5=""; L6=""; L7=""
    L1="Choose another location ? (yes/no) [yes]: \c"
    two_value_question yes no
    if [ "$PROMPT_ANSWER" = "Y" ]; then
      continue
    else
      cleanup 20
    fi
  fi

  # Check if destination directory exists, remove the whole thing!!!
  if [ -d $JRE_LOCATION ]; then
    if [ -d $JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION} ]; then
      L4=""; L5=""; L6=""; L7=""
      L1="The JRE Directory \"$JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION}\" already exists."
      L2="To avoid any possible problems, this directory should be cleaned before installation."
      L3="Remove all files in $JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION}? (yes/no) [yes]: \c"
      two_value_question yes no
      if [ "$PROMPT_ANSWER" = "Y" ]; then

        rm -rf $JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION}
        if [ $? -ne 0 ]; then
          echo
          echo "$0: ERROR: following system command failed:"
          echo "\"rm -rf $JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION}\""
          echo "Unable to remove an old installation of the JRE."
          echo
          cleanup 25
        fi

      fi
    fi

    UNINSTALL_LINE1="rm -rf $JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION}"

  else

    mkdir $JRE_LOCATION
    if [ $? -ne 0 ]; then
      echo
      echo "$0: ERROR: following system command failed:"
      echo "\"mkdir $JRE_LOCATION\""
      echo "Unable to create a new directory for the JRE."
      echo
      cleanup 30
    fi

    UNINSTALL_LINE1="rm -rf $JRE_LOCATION"

  fi

  cp ./$SOLARIS_JRE_RUNTIME $JRE_LOCATION
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"cp  ./$SOLARIS_JRE_RUNTIME $JRE_LOCATION\""
    echo "Unable to install JRE."
    echo
    cleanup 35
  fi

  chmod a+x $JRE_LOCATION/$SOLARIS_JRE_RUNTIME
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"chmod a+x $JRE_LOCATION/$SOLARIS_JRE_RUNTIME\""
    echo "Unable to install JRE."
    echo
    cleanup 40
  fi

  (cd $JRE_LOCATION;./$SOLARIS_JRE_RUNTIME)
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"(cd $JRE_LOCATION;./$SOLARIS_JRE_RUNTIME)\""
    echo "Unable to install JRE."
    echo
    cleanup 45
  fi

  # Remove the local copy of the JRE Runtime
  rm -rf $JRE_LOCATION/$SOLARIS_JRE_RUNTIME
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"rm -rf $JRE_LOCATION/$SOLARIS_JRE_RUNTIME\""
    echo "Unable to install JRE."
    echo
    cleanup 50
  fi

  JRE_LOCATION=$JRE_LOCATION/jre${CYBERCLASSROOM_JRE_VERSION}

fi
break
done

# Now install the actuall CyberClassroom Viewer and the Deitel Book
# Ask if the want to install it locally or leave it on the CDROM.         
# Check for previous CyberClassroom and remove if older.
# Check to see if there is space to install.
# Finally, write the commands necessary to do the unistallation script.

echo
echo "You are about to proceed with the Cyber Classroom installation."
echo
echo "\"Compact\" Installation requires approximately $CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_MB_FOR_COMPACT MB of disk space."
echo "All of the data files are installed on your local hard disk, EXCEPT"
echo "the audio files, which remain on the CD-ROM.  Links to these large"
echo "files are created on your hard drive.  This means you MUST have the "
echo "CD-ROM in your CD-ROM drive in order to run the Cyber Classroom."
echo
echo "\"Full\" Installation requires approximately $CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_MB_FOR_FULL MB of disk space."
echo "All of the data files are installed your local hard disk, INCLUDING"
echo "the audio files.  You do not need the CD-ROM in your CD-ROM drive"
echo "to run the Cyber Classroom."
echo

L2=""; L3=""; L4=""; L5=""; L6=""; L7=""
L1="Which installation option do you choose ? (Compact/Full) [Compact]: \c"
two_value_question Compact Full
if [ "$PROMPT_ANSWER" = "C" ]; then
  INSTALLATION_OPTION="Compact"
else
  INSTALLATION_OPTION="Full"
fi


# OK installation type determined lets check disk space 

if [ "$INSTALLATION_OPTION" = "Compact" ]; then
  CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K=$CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K_FOR_COMPACT
else
  CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K=$CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K_FOR_FULL
fi

echo
echo "Installation Mode \"$INSTALLATION_OPTION\" Chosen....."
echo


while [ 1 ]
do
  CYBERCLASSROOM_LOCATION=/opt/CyberClassroom
  echo
  echo "Note: If you have done previous installation of any CyberClassroom"
  echo "product, it is suggested you use the same directory as before."
  echo "This will eliminate duplicate copies of the CyberClassroom Viewer"
  echo "Where do we install the CyberClassroom (full directory path) ? [$CYBERCLASSROOM_LOCATION]: \c"

  read RESPONSE

  # if it's not null then don't use the default prompt answer
  if [ "$RESPONSE" != "" ]; then
    CYBERCLASSROOM_LOCATION=$RESPONSE
  fi  

  # Check to see if the main directory exists
  if [ ! -d $CYBERCLASSROOM_LOCATION ]; then

    L4=""; L5=""; L6=""; L7=""
    L1="The directory you have chosen"
    L2="\"$CYBERCLASSROOM_LOCATION\""
    L3="does not exist. Create Directory Now ? (yes/no) [yes]: \c"
    two_value_question yes no
    if [ "$PROMPT_ANSWER" = "Y" ]; then

      mkdir $CYBERCLASSROOM_LOCATION
      if [ $? -ne 0 ]; then
        echo
        echo "$0: ERROR: following system command failed:"
        echo "\"mkdir $CYBERCLASSROOM_LOCATION\""
        echo "Unable to create directory <$CYBERCLASSROOM_LOCATION>."
        echo
        DIR_OK=0
      else
        UNINSTALL_LINE4="rm -rf $CYBERCLASSROOM_LOCATION"
        DIR_OK=1
      fi
    # Prompt answer as no
    else 
      DIR_OK=0
    fi

    if [ $DIR_OK -eq 0 ]; then
      L2=""; L3=""; L4=""; L5=""; L6=""; L7=""
      L1="Choose another location? (yes/no) [yes]: \c"
      two_value_question yes no
      if [ "$PROMPT_ANSWER" = "Y" ]; then
        continue
      else
        cleanup 55
      fi
    fi
  fi

  # Check for required space in the selected CyberClassroom directory.  We do this by finding what
  # partition this directory is mounted on.  (Ex. /export/home/generic/CyberClassroom is on the /export/home
  # partition.

  echo
  echo "Checking Disk Space Requirements....."
  echo
  CURRENT_LOCATION_K=`df -k $CYBERCLASSROOM_LOCATION | tail -1 | awk '{print $4}'`
  if [  $CURRENT_LOCATION_K -le $CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K ]; then
    L5=""; L6=""; L7=""
    L1="WARNING: The directory you selected for $INSTALLATION_OPTION CyberClassroom installation "
    L2="\"$CYBERCLASSROOM_LOCATION\" only has ${CURRENT_LOCATION_K}K of available space."
    L3="in its partition. The CyberClassroom product requires ${CYBERCLASSROOM_BOOK_REQUIRED_SPACE_IN_K}K of space."
    L4="Select Another Location? (yes/no) [yes]: \c"
    two_value_question yes no
    if [ "$PROMPT_ANSWER" = "N" ]; then
      cleanup 60
    fi
  else 
    # Space test succeeded
    break
  fi

done

# Do check for a Previous CyberClassroom Viewer and ask (or recommend) to replace it if is
# identified as an old version

INSTALL_NEW_VIEWING_ENGINE=0
if [ -d $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR ]; then

  CURRENT_CYBERCLASSROOM_VERSION=`$JRE_LOCATION/bin/jre -cp $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR/gsl.jar gsl.gui.Classroom -version`
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following script command \"$JRE_LOCATION/bin/jre -cp "
    echo "$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR/gsl.jar gsl.gui.Classroom"
    echo " -version\" failed."
    echo "Unable to determine an existing CyberClassroom Viewer version number."
    echo "A new Viewer will be installed to correct the damaged installation."
    echo
    INSTALL_NEW_VIEWING_ENGINE=1
  else
    if [ `echo $CURRENT_CYBERCLASSROOM_VERSION | awk -F. '{print $1$2$3$4}'` -lt `echo $CYBERCLASSROOM_VIEWER_VERSION | awk -F. '{print $1$2$3$4}'` ]; then
      L6=""; L7=""
      L1="Note: The installation script has determined that you already have a previous installation of"
      L2="the CyberClassroom Viewing Engine (i.e. the "$CYBERCLASSROOM_VIEWER_DIR" directory) and determined that its version"
      L3="($CURRENT_CYBERCLASSROOM_VERSION) is older than the one being installed now ($CYBERCLASSROOM_VIEWER_VERSION)."
      L4="It is STRONGLY recommended that you install the updated version of this viewing engine."
      L5="Shall we proceed and install the new CyberClassroom Viewing Engine ? (yes/no) [yes]: \c"
      two_value_question yes no
      if [ "$PROMPT_ANSWER" = "Y" ]; then
        INSTALL_NEW_VIEWING_ENGINE=1
      fi
    fi
  fi
else
  INSTALL_NEW_VIEWING_ENGINE=1
fi

if [ $INSTALL_NEW_VIEWING_ENGINE -eq 1 ]; then

  if [ -d $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR ]; then
    rm -rf $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR
    if [ $? -ne 0 ]; then
      echo
      echo "$0: ERROR: following system command failed:"
      echo "\"rm -rf $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR\""
      echo "Unable to remove an old CyberClassroom Viewing Engine."
      echo
      cleanup 65
    fi
  fi

  echo
  echo "Copying Viewer Files....."
  echo

  cp -R $CYBERCLASSROOM_VIEWER_DIR $CYBERCLASSROOM_LOCATION
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"cp -R $CYBERCLASSROOM_VIEWER_DIR $CYBERCLASSROOM_LOCATION\""
    echo "Unable to install an new CyberClassroom Viewing Engine."
    echo
    cleanup 70
  fi

  UNINSTALL_LINE3="rm -rf $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR"

fi



# Now, install the book checking for "Compact" or "Full" Installation
# If already there, then remove it first and replace with a new one

INSTALL_NEW_BOOK_DIR=0
if [ -d $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR ]; then
  L5=""; L6=""; L7=""
  L1="Note: The installation script has determined that you already"
  L2="have a previous installation of the CyberClassroom Book"
  L3="directory \"$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR\"."
  L4="Should we reinstall over this directory (answer \"no\" to abort this script) ? (yes/no) [yes]: \c"
  two_value_question yes no
  if [ "$PROMPT_ANSWER" = "Y" ]; then
    INSTALL_NEW_BOOK_DIR=1
  else
    cleanup 73
  fi
else
  INSTALL_NEW_BOOK_DIR=1
fi

if [ $INSTALL_NEW_BOOK_DIR -eq 1 ]; then

  if [ -d $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR ]; then
    rm -rf $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR
    if [ $? -ne 0 ]; then
      echo
      echo "$0: ERROR: following system command failed:"
      echo "\"rm -rf $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR\""
      echo "Unable to remove an old installation of this CyberClassroom book."
      echo
      cleanup 75
    fi
  fi

  echo
  echo "Copying Book Files....."
  echo

  # First copy the contents of the "cpphtp2" directory without the jar files, zip files and answers directory. 
  # should use tar method creates the main directory as well as the sub directories if necessary.

  tar cvf - `ls -d $CYBERCLASSROOM_BOOK_DIR/* | egrep -v '\.zip$|\.jar$|\/answers$'` | ( cd $CYBERCLASSROOM_LOCATION; tar xvf - )
  if [ $? -ne 0 ]; then
     echo
     echo "$0: ERROR: following system command failed:"
     echo "tar cvf - `ls -d $CYBERCLASSROOM_BOOK_DIR/* | egrep -v '\.zip$|\.jar$|\/answers$'` | ( cd $CYBERCLASSROOM_LOCATION; tar xvf - )"
     echo "Unable to install the CyberClassroom book."
     echo
     cleanup 80
  fi

  UNINSTALL_LINE2="$UNINSTALL_LINE2 rm -rf $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR"

  # For a "Full" installation, copy the jar and zip files along with the answers directory.
  if [ "$INSTALLATION_OPTION" = "Full" ]; then
    tar cvf - `ls -d $CYBERCLASSROOM_BOOK_DIR/*.jar $CYBERCLASSROOM_BOOK_DIR/*.zip $CYBERCLASSROOM_BOOK_DIR/answers` | ( cd $CYBERCLASSROOM_LOCATION; tar xvf - )
    if [ $? -ne 0 ]; then
       echo
       echo "$0: ERROR: following system command failed:"
       echo "\"tar cvf - `ls $CYBERCLASSROOM_BOOK_DIR/*.jar` | ( cd $CYBERCLASSROOM_LOCATION; tar xvf - )\""
       echo "Unable to install the CyberClassroom book file ($CYBERCLASSROOM_BOOK_FILE)."
       echo
       cleanup 83
    fi
  fi

  echo
  echo "Making Setup Files....."
  echo


  # Make sure the "cpphtp2/webdb.ini" file is properly initialized by
  # recreating it here with its default values.  Also make sure its writtable
  # by any user.
  rm -f  $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_INITIALIZATION_FILE
  cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_INITIALIZATION_FILE
0
0
0
true
EOF

  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_INITIALIZATION_FILE\""
    echo "Unable to create the default setup file ($CYBERCLASSROOM_INITIALIZATION_FILE) for the CyberClassroom book."
    echo
    cleanup 100
  fi

  chmod a+w $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_INITIALIZATION_FILE
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"chmod a+w $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_INITIALIZATION_FILE\""
    echo "Unable to change permissions on the default setup file for the CyberClassroom book."
    echo
    cleanup 105
  fi

  # Create the "cpphtp2.htm" file based on the installation option.

  rm -f  $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE
  cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE
<hr>
<applet code=gsl.gui.Starter width=621 height=421>
EOF
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE\""
    echo "Unable to create main html file for the CyberClassroom book."
    echo
    cleanup 110
  fi

  if [ "$INSTALLATION_OPTION" = "Full" ]; then
    cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE
<param name=gmlarch value="$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/cpphtp2.jar;$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/examples.jar;$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/intro.zip;$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/code.jar;$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/iicons.jar;$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/graphics.jar;$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/bckgrnds.jar;$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/audio.zip;">
EOF
    if [ $? -ne 0 ]; then
      echo
      echo "$0: ERROR: following system command failed:"
      echo "\"cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE\""
      echo "Unable to create main html file for the CyberClassroom book."
      echo
      cleanup 111
    fi
  else
    cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE
<param name=gmlarch value="$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/cpphtp2.jar;$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/intro.zip;$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/examples.jar;$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/code.jar;$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/iicons.jar;$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/graphics.jar;$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/bckgrnds.jar;$CDROM_DIR/$CYBERCLASSROOM_BOOK_DIR/audio.zip;">
EOF
    if [ $? -ne 0 ]; then
      echo
      echo "$0: ERROR: following system command failed:"
      echo "\"cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE\""
      echo "Unable to create main html file for the CyberClassroom book."
      echo
      cleanup 112
    fi
  fi

  cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE
<param name=book value="cpphtp2.gmlb">
<param name=lastindex value=38>
<param name=fontsize value="16">
<param name=starter value="gsl.gui.vb.VBStarter">
<param name=aliases value="jarexe=xterm -e /bin/sh ">
<param name=jardir value="ism:examples/">
</applet>
<hr>
EOF
  if [ $? -ne 0 ]; then
    echo
    echo "$0: ERROR: following system command failed:"
    echo "\"cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE\""
    echo "Unable to create main html file for the CyberClassroom book."
    echo
    cleanup 113
  fi


fi # if [ $INSTALL_NEW_BOOK_DIR -eq 1 ]

# Now, build the script which will launch the CyberClassroom product for the user
# Again, make sure that its executable by every user.

cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_SCRIPT
PATH=$JRE_LOCATION/bin:$PATH:/usr/openwin/bin
export PATH
jre -cp $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR/gsl.jar:$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR/ism.jar:$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR/ui.jar:$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_VIEWER_DIR/search.jar gsl.gui.Classroom $CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_HTML_FILE &
EOF
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_SCRIPT\""
  echo "Unable to create startup script for the CyberClassroom book."
  echo
  cleanup 120
fi

chmod a+x $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_SCRIPT
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"chmod a+x $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_SCRIPT\""
  echo "Unable to create startup script for the CyberClassroom book."
  echo
  cleanup 125
fi

mkdir $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/CyberClassroom
mkdir $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/CyberClassroom/cpphtp2
mkdir $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/CyberClassroom/cpphtp2/examples
mkdir $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/CyberClassroom/cpphtp2/examples/Source
cp src/*.jar $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/CyberClassroom/cpphtp2/examples/Source




# Build a script for uninstallation operation

cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT
#!/bin/sh
QUIT_WHILE=0
while [ \$QUIT_WHILE -eq 0 ]
do
  echo
  echo "WARNING: This Prentice Hall CyberClassroom uninstallation script"
  echo "will run several system remove commands with verification (assuming you"
  echo "have file permissions to do so of course):"
  echo
  echo "It is recommended that you move this script to a location (such as /tmp) other"
  echo "than a CyberClassroom related directory.  By default, this script is installed"
  echo "in the main CyberClassroom directory.  Since this uninstallation script gives"
  echo "you the option to remove the main CyberClassroom directory in which it resides,"
  echo "you must move this script from there in order for the system remove command to succeed."
  echo
  echo "Do you want to continue ? (yes|no) [no]: \c"
  read YES_OR_NO
  if [ "\$YES_OR_NO" = "" ]; then
    YES_OR_NO="no"
  fi
  PROMPT_ANSWER=\`echo \$YES_OR_NO | tr "[:lower:]" "[:upper:]" | awk '{print substr(\$1,1,1)}'\`
  if [ "\$PROMPT_ANSWER" != "Y" -a "\$PROMPT_ANSWER" != "N" ]; then
    echo 
    echo "Please answer with \\"yes\\" or \\"no\\".  Try again."
  else
    QUIT_WHILE=1
  fi
done
if [ "\$PROMPT_ANSWER" != "Y" ]; then
  echo
  echo "Uninstallation Aborted."
  echo
  exit 1
fi
EOF
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT\""
  echo "Unable to create uninstallation script for the CyberClassroom book."
  echo
  cleanup 130
fi

if [ "$UNINSTALL_LINE2" != "" ]; then
cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT
QUIT_WHILE=0
while [ \$QUIT_WHILE -eq 0 ]
do
  echo
  echo "Remove the CyberClassroom book directory with the system remove command(s):"
  echo "\\"$UNINSTALL_LINE2\\" ? (yes/no) [yes]: \c"
  read YES_OR_NO
  if [ "\$YES_OR_NO" = "" ]; then
    YES_OR_NO="yes"
  fi
  PROMPT_ANSWER=\`echo \$YES_OR_NO | tr "[:lower:]" "[:upper:]" | awk '{print substr(\$1,1,1)}'\`
  if [ "\$PROMPT_ANSWER" != "Y" -a "\$PROMPT_ANSWER" != "N" ]; then
    echo 
    echo "Please answer with \\"yes\\" or \\"no\\".  Try again."
  else
    QUIT_WHILE=1
  fi
done
if [ "\$PROMPT_ANSWER" != "N" ]; then
  echo
  echo "$UNINSTALL_LINE2"
  $UNINSTALL_LINE2
fi
EOF
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"cat << EOF > >$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT\""
  echo "Unable to create uninstallation script for the CyberClassroom book."
  echo
  cleanup 135
fi
fi

if [ "$UNINSTALL_LINE3" != "" ]; then
cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT
QUIT_WHILE=0
while [ \$QUIT_WHILE -eq 0 ]
do
  echo
  echo "(WARNING: If you answer yes to the following question, previous CyberClassroom"
  echo "products may no longer work !!!)"
  echo
  echo "Remove the CyberClassroom viewing engine directory with the system remove command:"
  echo "\\"$UNINSTALL_LINE3\\" ? (yes/no) [yes]: \c"
  read YES_OR_NO
  if [ "\$YES_OR_NO" = "" ]; then
    YES_OR_NO="yes"
  fi
  PROMPT_ANSWER=\`echo \$YES_OR_NO | tr "[:lower:]" "[:upper:]" | awk '{print substr(\$1,1,1)}'\`
  if [ "\$PROMPT_ANSWER" != "Y" -a "\$PROMPT_ANSWER" != "N" ]; then
    echo 
    echo "Please answer with \\"yes\\" or \\"no\\".  Try again."
  else
    QUIT_WHILE=1
  fi
done
if [ "\$PROMPT_ANSWER" != "N" ]; then
  echo
  echo "$UNINSTALL_LINE3"
  $UNINSTALL_LINE3
fi
EOF
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"cat << EOF > >$CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT\""
  echo "Unable to create uninstallation script for the CyberClassroom book."
  echo
  cleanup 140
fi
fi

if [ "$UNINSTALL_LINE4" != "" ]; then
cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT
QUIT_WHILE=0
while [ \$QUIT_WHILE -eq 0 ]
do
  echo
  echo "(WARNING: If you answer yes to the following question, previous CyberClassroom"
  echo "products may also be removed !!!)"
  echo
  echo "Remove the entire CyberClassroom directory with system remove command:"
  echo "\\"$UNINSTALL_LINE4\\" ? (yes/no) [no]: \c"
  read YES_OR_NO
  if [ "\$YES_OR_NO" = "" ]; then
    YES_OR_NO="no"
  fi
  PROMPT_ANSWER=\`echo \$YES_OR_NO | tr "[:lower:]" "[:upper:]" | awk '{print substr(\$1,1,1)}'\`
  if [ "\$PROMPT_ANSWER" != "Y" -a "\$PROMPT_ANSWER" != "N" ]; then
    echo 
    echo "Please answer with \\"yes\\" or \\"no\\".  Try again."
  else
    QUIT_WHILE=1
  fi
done
if [ "\$PROMPT_ANSWER" != "N" ]; then
  echo
  echo "$UNINSTALL_LINE4"
  $UNINSTALL_LINE4
fi
EOF
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"cat << EOF > $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT\""
  echo "Unable to create uninstallation script for the CyberClassroom book."
  echo
  cleanup 145
fi
fi

# Add the uninstallation option to the uninstall script if
# this installation did infact install the JRE.
if [ "$UNINSTALL_LINE1" != "" ]; then
cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT
QUIT_WHILE=0
while [ \$QUIT_WHILE -eq 0 ]
do
  echo
  echo "(WARNING: If you answer yes to the following question, previous CyberClassroom"
  echo "products may no longer execute properly !!!)"
  echo
  echo "Remove the entire JRE directory with system remove command"
  echo "\\"$UNINSTALL_LINE1\\" ? (yes/no) [no]: \c"
  read YES_OR_NO
  if [ "\$YES_OR_NO" = "" ]; then
    YES_OR_NO="no"
  fi
  PROMPT_ANSWER=\`echo \$YES_OR_NO | tr "[:lower:]" "[:upper:]" | awk '{print substr(\$1,1,1)}'\`
  if [ "\$PROMPT_ANSWER" != "Y" -a "\$PROMPT_ANSWER" != "N" ]; then
    echo 
    echo "Please answer with \\"yes\\" or \\"no\\".  Try again."
  else
    QUIT_WHILE=1
  fi
done
if [ "\$PROMPT_ANSWER" != "N" ]; then
  echo
  echo "$UNINSTALL_LINE1"
  $UNINSTALL_LINE1
fi
EOF
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT\""
  echo "Unable to create uninstallation script for the CyberClassroom book."
  echo
  cleanup 150
fi
fi

# add an extra echo line to the uninstall script
cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT
echo
EOF
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"cat << EOF >> $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT\""
  echo "Unable to create uninstallation script for the CyberClassroom book."
  echo
  cleanup 155
fi

# make the uninstall script executable
chmod a+x $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT
if [ $? -ne 0 ]; then
  echo
  echo "$0: ERROR: following system command failed:"
  echo "\"chmod a+x $CYBERCLASSROOM_LOCATION/$CYBERCLASSROOM_BOOK_DIR/$CYBERCLASSROOM_BOOK_UNINSTALL_SCRIPT\""
  echo "Unable to create uninstallation script for the CyberClassroom book."
  echo
  echo "Installation Aborted, cleaning up..."
  echo
  cleanup 160
fi

# Finalization screen

echo
echo "The Prentice Hall Deitel CyberClassroom has been sucessfully installed!"
echo "Change your directory to \"$CYBERCLASSROOM_LOCATION\""
echo "(cd $CYBERCLASSROOM_LOCATION) and execute the script"
echo "\"$CYBERCLASSROOM_BOOK_SCRIPT\" (./$CYBERCLASSROOM_BOOK_SCRIPT) to run the program."
echo



